﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;

/*
 * zhengdong
 */
namespace BTS.COMMON
{
    public class BTSCache
    {
        /// <summary>
        /// 简单缓存存储
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Insert(string key, string value)
        {
            Cache cache = new Cache();
            cache.Insert(key, value);
        }

        /// <summary>
        /// 依赖文件的缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="fileName"></param>
        public static void Insert(string key, string value, string fileName)
        {
            Cache cache = new Cache();
            cache.Insert(key, value, new CacheDependency(HttpContext.Current.Server.MapPath(fileName)));
        }

        /// <summary>
        /// 设置过期时间的缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="type">时间类型1：秒，2：分，3：时，default：天</param>
        /// <param name="num">时间量</param>
        /// <param name="isAbsolute">true：绝对缓存，false：滑动缓存</param>
        public static void Insert(string key, string value, int type, int num, bool isAbsolute)
        {
            Cache cache = new Cache();
            switch (type)
            {
                case 1:
                    if (isAbsolute)
                        cache.Insert(key, value, null, DateTime.Now.AddSeconds(num), TimeSpan.Zero);
                    else
                        cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(num));
                    break;
                case 2:
                    if (isAbsolute)
                        cache.Insert(key, value, null, DateTime.Now.AddMinutes(num), TimeSpan.Zero);
                    else
                        cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(num));
                    break;
                case 3:
                    if (isAbsolute)
                        cache.Insert(key, value, null, DateTime.Now.AddHours(num), TimeSpan.Zero);
                    else
                        cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(num));
                    break;
                default:
                    if (isAbsolute)
                        cache.Insert(key, value, null, DateTime.Now.AddDays(num), TimeSpan.Zero);
                    else
                        cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromDays(num));
                    break;
            }
        }

        /// <summary>
        /// 删除某个缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            Cache cache = new Cache();
            cache.Remove(key);
        }
    }
}
